//
//  YWDrawView.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/7/26.
//

#import "YWDrawView.h"


@interface YWDrawView ()

@property (nonatomic, strong) NSMutableArray *sequenceArray; //顺序数组

@property (nonatomic, strong) NSMutableArray *showArray; //显示数组

@end

@implementation YWDrawView


- (NSMutableArray *)sequenceArray {
    
    if (_sequenceArray == nil) {
        _sequenceArray = [NSMutableArray array];
    }
    return _sequenceArray;
}

- (NSMutableArray *)showArray {
    
    if (_showArray == nil) {
        _showArray = [NSMutableArray array];
    }
    return _showArray;
}

//纯代码用这个
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

//xib用这个
-(void)awakeFromNib{
    [super awakeFromNib];
    [self setup];
}

//初始化
-(void)setup{
    
    
}

//添加图层
-(void)addImage:(UIImage *)image withType:(YWLayerStatusType)type{
    
    //创建模型
    YWLayerStatus *status = [[YWLayerStatus alloc] init];
    status.image = image;
    status.type = type;
    
    //从顺序数组截取显示数组的数量、然后才添加到顺序数组
    self.sequenceArray = [NSMutableArray arrayWithArray:[self.sequenceArray subarrayWithRange:NSMakeRange(0, self.showArray.count)]];
    
    [self.sequenceArray addObject:status];
    
    //然后显示数组再从顺序数组获取同等数据
    self.showArray = [NSMutableArray arrayWithArray:self.sequenceArray];
    
    //重绘
    [self setNeedsDisplay];
    
}

//撤销图层 (不要动顺序数组、只要动显示数组)
-(void)previousHandle{
    
    if (self.showArray.count > 1) {
        
        [self.showArray removeLastObject];
        
        //重绘
        [self setNeedsDisplay];
    }
    
}

//前进图层 (不要动顺序数组、只要动显示数组)
-(void)nextHandle{
    
    if (self.showArray.count < self.sequenceArray.count) {
        
        YWLayerStatus *nextStatus = self.sequenceArray[self.showArray.count];
        
        [self.showArray addObject:nextStatus];
        
        //重绘
        [self setNeedsDisplay];
    }
    
}

//重置图层
-(void)removeAllHandle{
    
    if (self.sequenceArray.count > 0) {
        
        YWLayerStatus *firstStatus = self.sequenceArray[0];
        //删剩最后一个模型就好
        [self.sequenceArray removeAllObjects];
        self.sequenceArray = [NSMutableArray arrayWithArray:@[firstStatus]];
        
        //然后显示数组再从顺序数组获取同等数据
        self.showArray = [NSMutableArray arrayWithArray:self.sequenceArray];
        
        //重绘
        [self setNeedsDisplay];
    }
    
}


//获取当前图层
-(UIImage *)getCurrentLayer{
    
    //1.开启一个位图上下文
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
    
    //2.把画板上的内容渲染到上下文当中
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    [self.layer renderInContext:ctx];
    
    //3.从上下文当中取出一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //4.关闭上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}

//重绘
-(void)drawRect:(CGRect)rect {
    
    //遍历图层模型来重绘（这里全都是“图层”模型、不会有贝塞尔曲线之类的、所以用image来drawInRect就行）
    for (YWLayerStatus *status in self.showArray) {
        [status.image drawInRect:rect];
      
    }
    
}


@end
